<?php

namespace api\modules\v1\controllers;
use api\common\controllers\CommonController;
use api\common\models\User;
use api\modules\v1\models\Category;
use api\modules\v1\models\ReportStatus;
use yii\web\ServerErrorHttpException;
use yii\web\HttpException;
use api\modules\v1\models\Code;
use api\common\models\Common;
use Yii;
use yii\helpers\ArrayHelper;
class LoginController extends CommonController
{
    public $modelClass = 'api\modules\v1\models\Login';


    public function actions()
    {
        $actions = parent::actions();
        unset($actions['index'],$actions['update'], $actions['create'], $actions['delete'], $actions['view']); //卸载操作
        return $actions;
    }


    /*
     * 登录账号
     * @return mixed
     */
    public function actionCreate()
     {
         $model = new $this->modelClass();
         $model->scenario='login';
         $model->attributes = Yii::$app->request->post();
         if ($model->login()) {
             $user = User::findByUsername($model->username);
             //$Category = new Category();
             //$user->collect_category = $Category->getId2Info($user->collect_category);
             $userinfo = $user->attributes;
             $userinfo['has_pwd'] = 0;
             if($userinfo['password_hash'] != ''){
                 $userinfo['has_pwd'] = 1;
             }

             unset($userinfo['auth_key']);
             unset($userinfo['password_hash']);
             unset($userinfo['password_reset_token']);
             unset($userinfo['email']);
             unset($userinfo['collect_category']);
             return $userinfo;
         }elseif (!$model->hasErrors()) {
             throw new ServerErrorHttpException('由于不明原因未能创建对象.');
         }else{
             throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
         }
     }


    /*
     * 登录账号
     * @return mixed
     */
    public function actionThird()
    {
        $model = new $this->modelClass();
        $model->scenario='third';
        $model->attributes = Yii::$app->request->post();
        if ($user = $model->loginThird()) {
            $userinfo = $user->attributes;
            $userinfo['has_pwd'] = 0;
            if($userinfo['password_hash'] != ''){
                $userinfo['has_pwd'] = 1;
            }

            unset($userinfo['auth_key']);
            unset($userinfo['password_hash']);
            unset($userinfo['password_reset_token']);
            unset($userinfo['email']);
            unset($userinfo['collect_category']);
            return $userinfo;
        }elseif (!$model->hasErrors()) {
            throw new ServerErrorHttpException('由于不明原因未能创建对象.');
        }else{
            throw new HttpException(401,array_values($model->getFirstErrors())[0],0);
        }
    }

}
